home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / hash / shs.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-25  |  5.1 KB  |  191 lines  |  [TEXT/R*ch]

  1. /* @(#)shs.h    10.1 3/25/94 08:04:01 */
  2. /*
  3.  * shs - NIST proposed Secure Hash Standard
  4.  *
  5.  * Written 2 September 1992, Peter C. Gutmann.
  6.  *
  7.  * This file was Modified by:
  8.  *
  9.  *     Landon Curt Noll  (chongo@toad.com)    chongo <was here> /\../\
  10.  *
  11.  * This code has been placed in the public domain.  Please do not
  12.  * copyright this code.
  13.  *
  14.  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH  REGARD  TO
  15.  * THIS  SOFTWARE,  INCLUDING  ALL IMPLIED WARRANTIES OF MER-
  16.  * CHANTABILITY AND FITNESS.  IN NO EVENT SHALL  LANDON  CURT
  17.  * NOLL  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  18.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM  LOSS  OF
  19.  * USE,  DATA  OR  PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  20.  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR  IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * See shsdrvr.c for version and modification history.
  24.  */
  25.  
  26. #if !defined(SHS_H)
  27. #define SHS_H
  28.  
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31.  
  32. /*
  33.  * determine if we are checked in
  34.  */
  35. #define SHS_H_WHAT "@(#)"    /* @(#) if checked in */
  36.  
  37. /*
  38.  * Useful defines/typedefs
  39.  */
  40. typedef unsigned char BYTE;    /* must be a 1 byte unsigned value */
  41. typedef unsigned long UINT;    /* must be a 2 byte unsigned value */
  42. typedef unsigned long ULONG;    /* must be a 4 byte unsigned value */
  43.  
  44. /* SHS_CHUNKSIZE must be a power of 2 - fixed value defined by the algorithm */
  45. #define SHS_CHUNKSIZE (1<<6)
  46. #define SHS_CHUNKWORDS (SHS_CHUNKSIZE/sizeof(ULONG))
  47.  
  48. /* SHS_DIGESTSIZE is a the length of the digest as defined by the algorithm */
  49. #define SHS_DIGESTSIZE (20)
  50. #define SHS_DIGESTWORDS (SHS_DIGESTSIZE/sizeof(ULONG))
  51.  
  52. /* SHS_LOW - where low 32 bits of 64 bit count is stored during final */
  53. #define SHS_LOW 15
  54.  
  55. /* SHS_HIGH - where high 32 bits of 64 bit count is stored during final */
  56. #define SHS_HIGH 14
  57.  
  58. /*
  59.  * MAXBLOCK - maximum blocking factor
  60.  *
  61.  * must be power of 2 > SHS_CHUNKSIZE and <= READSIZE and < 2^29
  62.  */
  63. #define MAXBLOCK (SHS_CHUNKSIZE<<9)
  64.  
  65. /* READSIZE must be a multiple of SHS_CHUNKSIZE >= MAXBLOCK */
  66. #define READSIZE (SHS_CHUNKSIZE<<9)
  67. #define READWORDS (READSIZE/sizeof(ULONG))
  68.  
  69. /* maximum part of pre_file used - must be <= MAXBLOCK */
  70. #define MAX_PRE_FILE MAXBLOCK
  71.  
  72. /*
  73.  * COUNT(SHS_INFO*, ULONG) - update the 64 bit count in an SHS_INFO
  74.  *
  75.  * We will count bytes and convert to bit count during the final
  76.  * transform.
  77.  */
  78. #define COUNT(shsinfo, count) {                    \
  79.     ULONG tmp_countLo;                        \
  80.     tmp_countLo = (shsinfo)->countLo;                \
  81.     if (((shsinfo)->countLo += (count)) < tmp_countLo) {    \
  82.     (shsinfo)->countHi++;                    \
  83.     }                                \
  84. }
  85.  
  86. /*
  87.  * SWAP_BYTE_SEX(ULONG *dest, ULONG *src)
  88.  *
  89.  *    dest    - array of SHS_CHUNKWORDS ULONG of fixed data (may be src)
  90.  *    src    - array of SHS_CHUNKWORDS ULONG of what to fix
  91.  *
  92.  * This macro will either switch to the opposite byte sex (Big Endian vs.
  93.  *  Little Endian).
  94.  */
  95. #define SWAP_BYTE_SEX(dest, src) {    /* swap byte sex if needed */    \
  96.     int tmp_i;    /* index */                        \
  97.     for (tmp_i=0; tmp_i < SHS_CHUNKWORDS; ++tmp_i) {            \
  98.     ((ULONG*)(dest))[tmp_i] =                    \
  99.       (((ULONG*)(src))[tmp_i] << 16) |                \
  100.       (((ULONG*)(src))[tmp_i] >> 16);                \
  101.     ((ULONG*)(dest))[tmp_i] =                    \
  102.       ((((ULONG*)(dest))[tmp_i] & 0xff00ff00UL) >> 8) |        \
  103.       ((((ULONG*)(dest))[tmp_i] & 0x00ff00ffUL) << 8);        \
  104.     }                                    \
  105. }
  106.  
  107. /* 
  108.  * SHS_TRANSFORM(SHS_INFO *a, ULONG *b, ULONG *c)
  109.  *
  110.  *     a    pointer to our current digest state
  111.  *    b    pointer to SHS_CHUNKSIZE words of byte sex fixed data
  112.  *    c    pointer to SHS_CHUNKSIZE words that do not need to be fixed
  113.  */
  114. #if BYTE_ORDER == BIG_ENDIAN
  115. # define SHS_TRANSFORM(a,b,c)                        \
  116.     shsTransform(((SHS_INFO *)(a))->digest, (ULONG *)(c))
  117. #else
  118. # define SHS_TRANSFORM(a,b,c) {                     \
  119.     SWAP_BYTE_SEX((b), (c));                        \
  120.     shsTransform(((SHS_INFO *)(a))->digest, (ULONG *)(b));        \
  121. }
  122. #endif
  123.  
  124. /*
  125.  * ROUNDUP(x,y) - round x up to the next multiple of y
  126.  */
  127. #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
  128.  
  129. /*
  130.  * The structure for storing SHS info
  131.  *
  132.  * We will assume that bit count is a multiple of 8.
  133.  */
  134. typedef struct {
  135.     ULONG digest[SHS_DIGESTWORDS];    /* message digest */
  136.     ULONG countLo;            /* 64 bit count: bits 3-34 */
  137.     ULONG countHi;            /* 64 bit count: bits 35-63 */
  138.     ULONG datalen;            /* length of data in data */
  139.     ULONG data[SHS_CHUNKWORDS];        /* SHS chunk buffer */
  140. } SHS_INFO;
  141.  
  142. /*
  143.  * elements of the stat structure that we will process
  144.  */
  145. struct hashstat {
  146.     dev_t st_dev;
  147.     ino_t st_ino;
  148.     mode_t st_mode;
  149.     nlink_t st_nlink;
  150.     uid_t st_uid;
  151.     gid_t st_gid;
  152.     off_t st_size;
  153.     time_t st_mtime;
  154.     time_t st_ctime;
  155. };
  156.  
  157. /*
  158.  * Used to remove arguments in function prototypes for non-ANSI C
  159.  */
  160. #if defined(__STDC__) && __STDC__ == 1
  161. # define PROTO
  162. #endif
  163. #ifdef PROTO
  164. # define P(a) a
  165. #else    /* !PROTO */
  166. # define P(a) ()
  167. #endif    /* ?PROTO */
  168.  
  169. /* shs.c */
  170. void shsInit P((SHS_INFO*));
  171. void shsUpdate P((SHS_INFO*, BYTE*, ULONG));
  172. void shsfullUpdate P((SHS_INFO*, BYTE*, ULONG));
  173. void shsFinal P((SHS_INFO*));
  174. char *shs_what;
  175.  
  176. /* shsdual.c */
  177. void dualMain P((int, char**, BYTE*, UINT, char*));
  178. void dualTest P((void));
  179. char *shsdual_what;
  180.  
  181. /* shsdrvr.c */
  182. void shsPrint P((SHS_INFO*));
  183. ULONG zero[];
  184. char *program;
  185. int debug;
  186. int i_flag;
  187. int q_flag;
  188. int dot_zero;
  189.  
  190. #endif /* SHS_H */
  191.